package com.pwc.conversionTool.dataAdaptor;


public class StringUtil {
	 public static String ltrim(String str){
		 
		 if(" ".equals(str.substring(0,1))){
			 str = str.substring(1);
			 
			 return ltrim(str);
		 }else{
			 return str;
		 }	
	 }
	 
	 public static String rtrim(String str){
		 
		 if(" ".equals(str.substring(str.length()-1,str.length()))){
			 str = str.substring(0,str.length()-1);
			 
			 return rtrim(str);
		 }else{
			 return str;
		 }	
	 }
	 
	 public static String trim(String str){
		 
		 str=str.trim();
		 
		 return str;
		 
	 }
	 
	 public static String InitCap(String str){
		 if(!"".equals(str)){
		 str  = str.substring(0,1).toUpperCase()+str.substring(1);}
		 return str;
	 }
	 
	 public static String get_area_code(String str){
		 if(str.length()>=7){
		 str  = str.substring(4,7);}
		 return str;
	 }
	 
	 public static String get_phone_number(String str){
		 if(str.length()>=12){
		 str  = str.substring(9,12)+"-"+str.substring(str.length()-4,str.length());}
		 return str;
	 }
	 
	 
	 //need to check if oldchar="",then we cannot input newchar;
	 public static String Replace(String str,String oldChar,String newChar){
		 str  = str.replace(oldChar, newChar);
		 return str;
	 }
	 
	 //need to check index and length is integer in frontend/javascript.
	 public static String SubString(String str,String index,String length){
		 int indexN=Integer.parseInt(index);
		 int lengthN;
		 if("".equals(length)){
			 lengthN=Integer.MAX_VALUE;
		 }else{
			 lengthN=Integer.parseInt(length);
		 }
		 
		 int outofrange=indexN-1+lengthN;
		 int outofrange2=str.length()+indexN+lengthN;
		 if(lengthN<0){
			 str="";
		 }else{
			 if("".equals(length)){
				 if(indexN<0&&Math.abs(indexN)<=str.length()){
					 str=str.substring(str.length()+indexN,str.length());
				 }else if(indexN>0&&Math.abs(indexN)<=str.length()){
					 str=str.substring(indexN-1,str.length());
				 }else if(Math.abs(indexN)>str.length()){
					 str="";
				 }
				   
			 }else{
				 if(indexN<0&&Math.abs(indexN)<=str.length()){
					 if(outofrange2>str.length()){
						 outofrange2=str.length();
					 }
					 str=str.substring(str.length()+indexN,outofrange2);
				 }else if(indexN>0&&Math.abs(indexN)<=str.length()){
					 if(outofrange>str.length()){
						 outofrange=str.length();
					 }
					 str=str.substring(indexN-1,outofrange);
				 }else if(Math.abs(indexN)>str.length()){
					 str="";
				 }
		 }
		 
		 
			 	 
		 }
		
		 return str;
	 }

}
